﻿using System;
using System.Windows;

using iStudio.Structure.Engine;
using iStudio.Structure.Wave;

namespace iStudio.Module.Basic
{
    /// <summary>
    /// Interaction logic for BasicWindow.xaml
    /// </summary>
    public partial class BasicWindow
    {
        public BasicWindow(Structure.Engine.Module module) : base (module)
        {
            InitializeComponent();

            Title = "Basic functions";
        }

        private void ButtonAmplifyClick(object sender, RoutedEventArgs e)
        {
            int factor = Convert.ToInt32(amplify_factor.Text);

            //
            // Go through all selected wave chunks
            //

            foreach (WaveTrack track in AudioEngine.Instance.Project.TrackList)
            {
                foreach (WaveChunk chunk in track.TrackChunks)
                {
                    if (chunk.IsSelected)
                    {
                        //
                        // Set frame change to 128, so each sample changed will not be recalculated
                        //
                        chunk.WaveBuffer.SetFrameChange (128);

                        for (int i = 0; i < chunk.WaveBuffer.Count; i++)
                        {
                            float value = factor * chunk.WaveBuffer[i];
                            
                            //
                            // Cut away the samples over the limit
                            //

                            if (value > 1.0f)
                            {
                                value = 1.0f;
                            }
                            else if (value < -1.0f)
                            {
                                value = -1.0f;
                            }

                            chunk.WaveBuffer[i] = value;
                        }

                        chunk.WaveBuffer.SetFrameChange (0);

                        ModuleApi.ConsoleLog ("Amplified chunk with factor: " + factor.ToString("N2"));
                    }
                }
            }
        }

        private void ButtonNormalizeClick(object sender, RoutedEventArgs e)
        {
            const float max = 1.0f;

            foreach (WaveTrack track in AudioEngine.Instance.Project.TrackList)
            {
                foreach (WaveChunk chunk in track.TrackChunks)
                {
                    if (chunk.IsSelected)
                    {
                        //
                        // Finding maximum value
                        //

                        float maximumValue = chunk.WaveBuffer[0];
                        float minimumValue = chunk.WaveBuffer[0];

                        foreach (float t in chunk.WaveBuffer)
                        {
                            if (t > maximumValue)
                            {
                                maximumValue = t;
                            }

                            if (t < minimumValue)
                            {
                                minimumValue = t;
                            }
                        }

                        //
                        // Calculate the factor
                        //

                        float factor = max / maximumValue;

                        //
                        // Multiply the factor
                        //

                        chunk.WaveBuffer.SetFrameChange (128);

                        for (int i = 0; i < chunk.WaveBuffer.Count; i++)
                        {
                            chunk.WaveBuffer[i] = factor * chunk.WaveBuffer[i];
                        }

                        chunk.WaveBuffer.SetFrameChange (0);

                        ModuleApi.ConsoleLog("Normalized audio with factor: " + factor.ToString("N2"));
                    }
                }
            }
        }

        private void ButtonResampleClick(object sender, RoutedEventArgs e)
        {
            int desiredFrequency = Convert.ToInt32(resample_freq.Text);

            foreach (WaveTrack track in AudioEngine.Instance.Project.TrackList)
            {
                bool resample = false;

                foreach (WaveChunk chunk in track.TrackChunks)
                {
                    if (chunk.IsSelected)
                    {
                        resample = true;
                    }
                }

                if (resample)
                {
                    try
                    {
                        track.SampleRate = desiredFrequency;

                        //
                        // Load resampled audio back into audio engine
                        //

                        ModuleApi.ConsoleLog("Resampled audio with: " + desiredFrequency + " Hz");

                    }
                    catch (Exception ex)
                    {
                        ModuleApi.ConsoleLog("Resample error: " + ex.Message);
                    }
                }
            }
        }
    }
}
